package controller

import (
	"github.com/gin-gonic/gin"
	"guliedu/model"
	"net/http"
	"strconv"
)

type courseAndDescriptionInfo struct {
	ID              int    `json:"id"`
	Cover           string `json:"cover"`
	Description     string `json:"description"`
	LessonNum       int    `json:"lessonNum"`
	Price           int    `json:"price"`
	SubjectID       int    `json:"subjectId"`
	SubjectParentID int    `json:"subjectParentId"`
	TeacherID       int    `json:"teacherId"`
	Title           string `json:"title"`
}

type publishCourseInfo struct {
	Cover           string `json:"cover"`
	Title           string `json:"title"`
	LessonNum       int    `json:"lessonNum"`
	SubjectLevelOne string `json:"subjectLevelOne"`
	SubjectLevelTwo string `json:"subjectLevelTwo"`
	TeacherName     string `json:"teacherName"`
	Price           int    `json:"price"`
}

func AddCourse(ctx *gin.Context) {
	courseInfo := &courseAndDescriptionInfo{}
	ctx.ShouldBind(&courseInfo)

	course := &model.EduCourse{
		TeacherID:       courseInfo.TeacherID,
		SubjectID:       courseInfo.SubjectID,
		SubjectParentID: courseInfo.SubjectParentID,
		Title:           courseInfo.Title,
		Price:           courseInfo.Price,
		LessonNum:       courseInfo.Price,
		Cover:           courseInfo.Cover,
		BuyCount:        0,
		ViewCount:       0,
		Version:         0,
		Status:          "",
		Sort:            0,
		Description: model.EduCourseDescription{
			Description: courseInfo.Description,
		},
	}

	eduCourseService.Insert(course)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": course.ID,
	})
	return
}

func UploadCourseCover(ctx *gin.Context) {
	file, header, _ := ctx.Request.FormFile("file")
	url, code := eduCourseService.UploadCover(file, header.Size)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"url":  url,
	})
	return
}

func GetCourseInfo(ctx *gin.Context) {
	courseID := ctx.Param("id")
	course, _ := eduCourseService.Select(courseID)
	description, _ := eduCourseDescriptionService.SelectByCourseID(courseID)
	ctx.JSON(http.StatusOK, gin.H{
		"code":        20000,
		"data":        course,
		"description": description.Description,
	})
	return
}

func UpdateCourseInfo(ctx *gin.Context) {
	courseInfo := &courseAndDescriptionInfo{}
	ctx.ShouldBind(&courseInfo)

	converID := strconv.Itoa(courseInfo.ID)
	course, _ := eduCourseService.Select(converID)
	course.TeacherID = courseInfo.TeacherID
	course.SubjectID = courseInfo.SubjectID
	course.SubjectParentID = courseInfo.SubjectParentID
	course.Title = courseInfo.Title
	course.Price = courseInfo.Price
	course.LessonNum = courseInfo.LessonNum
	course.Cover = courseInfo.Cover
	eduCourseService.Update(course)

	description, _ := eduCourseDescriptionService.SelectByCourseID(converID)
	description.Description = courseInfo.Description
	eduCourseDescriptionService.Update(description)

	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func GetPublishCourse(ctx *gin.Context) {
	id := ctx.Param("id")

	course, _ := eduCourseService.Select(id)
	levelOneID := strconv.Itoa(course.SubjectParentID)
	levelTwoID := strconv.Itoa(course.SubjectID)
	teacherID := strconv.Itoa(course.TeacherID)
	levelOne, _ := eduSubjectService.SelectByCourseID(levelOneID)
	levelTwo, _ := eduSubjectService.SelectByCourseID(levelTwoID)
	teacher, _ := eduTeacherService.SelectByID(teacherID)
	publishCourse := &publishCourseInfo{
		Cover:           course.Cover,
		Title:           course.Title,
		LessonNum:       course.LessonNum,
		SubjectLevelOne: levelOne.Title,
		SubjectLevelTwo: levelTwo.Title,
		TeacherName:     teacher.Name,
		Price:           course.Price,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": publishCourse,
	})
	return
}

func PublishCourse(ctx *gin.Context) {
	id := ctx.Param("id")
	course, _ := eduCourseService.Select(id)
	course.Status = "Normal"
	eduCourseService.Update(course)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func SelectAllCourse(ctx *gin.Context) {
	courses, _ := eduCourseService.SelectAllCourse()
	ctx.JSON(http.StatusOK, gin.H{
		"code":  20000,
		"data":  courses,
		"total": len(courses),
	})
	return
}

func DeleteCourse(ctx *gin.Context) {
	courseID := ctx.Param("id")

	// 删除课程描述
	eduCourseDescriptionService.DeleteByCourseID(courseID)
	// 删除课程视频
	eduVideoService.DeleteByCourseID(courseID)
	// 删除课程章节
	eduChapterService.DeleteByCourseID(courseID)
	// 删除课程
	eduCourseService.Delete(courseID)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}
